import Vue from 'vue'
import request from "@/api/request";
import store from '@/store/index'
const { $Toast } = require("@/../static/iview/base/index");


export const path = "https://app.xingzaikeji.com/"

// 获取多商家首页列表
export const getIndexIndex = () => {
  getLocation().then(location => {
    request.getIndexIndex({
      longitude: location.longitude,
      latitude: location.latitude,
    }).then(res => {
      store.commit('setHomeIndex', res.data)
    });
  })
}

// 获取定位信息
export const getLocation = () => {
  return new Promise((resolve, reject) => {
    wx.getLocation({
      success: (res) => {
        store.commit('setLocation', res)
        getLocationName(res)
        resolve(res)
      },
      error: (error) => {
        console.log(res)
        reject(error)
      }
    })
  })
}

// 获取定位信息
export const getLocationName = (option) => {
  request.getLocationName({
    longitude: option.longitude,
    latitude: option.latitude,
  }).then(res => {
    store.commit('setLocationInfo', res.result)
  }).catch(res => {
    store.commit('setLocationInfo', res.result)
  })
}

// 获取首页列表
export const getIndexList = () => {
  request.getIndexIndex().then(res => {
    store.commit('setHomeList', res.data)
  });
}

// 请求商品分类列表
export const getMallList = () => {
  request.getMallList({
    page: 1,
    page_num: 99999
  }).then(res => {
    store.commit('setMallList', res.data)
  });
}

// 获取收藏列表
export const getCollects = (option) => {
  return new Promise((resolve, reject) => {
    request
      .getCollects(option)
      .then(res => {
        store.commit('setCollectsList', res.data);
        resolve(res)
      }).catch(error => reject(error));
  })
}

// 添加商品到收藏
export const getMallCollect = (data) => {
  return new Promise((resolve, reject) => {
    request.getMallCollect({
      goods_id: data.goods_id
    }).then(res => {
      console.log(res)
      getCollects()
      getIndexList()
      getMallList()
      $Toast({
        content: res.message,
        type: res.code == 0 ? "success" : "warning"
      })
      if (res.code == 0) {
        resolve(res)
      } else {
        reject(res)
      }
    }).catch(error => {
      $Toast({
        content: res.message,
        type: res.code == 0 ? "success" : "warning"
      })
      reject(res)
    })
  });
}

// 取消商品收藏
export const cancelCollect = (data) => {
  return new Promise((resolve, reject) => {
    request.cancelCollect({
      goods_id: data.goods_id
    }).then(res => {
      console.log(res)
      getCollects()
      getIndexList()
      getMallList()
      $Toast({
        content: res.message,
        type: res.code == 0 ? "success" : "warning"
      });
      if (res.code == 0) {
        resolve(res)
      } else {
        reject(res)
      }
    }).catch(error => {
      $Toast({
        content: res.message,
        type: res.code == 0 ? "success" : "warning"
      })
      reject(error)
    })
  });
}


// 添加店铺到收藏
export const storeCollect = (data) => {
  return new Promise((resolve, reject) => {
    request.storeCollect({
      store_id: data.store_id
    }).then(res => {
      console.log(res)
      console.log("收藏成222功")
      getCollects()
      getIndexList()
      getMallList()
      $Toast({
        content: res.message,
        type: res.code == 0 ? "success" : "warning"
      })
      if (res.code == 0) {
        resolve(res)
      } else {
        reject(res)
      }
    }).catch(error => {
      $Toast({
        content: res.message,
        type: res.code == 0 ? "success" : "warning"
      })
      reject(res)
    })
  });
}

// 取消店铺收藏
export const cancelStoreCollect = (data) => {
  return new Promise((resolve, reject) => {
    request.cancelStoreCollect({
      store_id: data.store_id
    }).then(res => {
      getCollects()
      getIndexList()
      getMallList()
      $Toast({
        content: res.message,
        type: res.code == 0 ? "success" : "warning"
      });
      if (res.code == 0) {
        resolve(res)
      } else {
        reject(res)
      }
    }).catch(error => {
      reject(error)
    })
  });
}

// 更新购物车
export const getMallCartList = (data) => {
  request.getMallCartList({
    page: 1,
    page_num: 9999,
  }).then(res => {
    store.commit('setCartList', res.data);
  })
};


// 获取用户信息
export const getUserInfo = () => {
  request.getUserInfo().then(res => {
    store.commit('setUserInfo', res.data)
  });
}

// 获取优惠券列表
export const getCouponsList = () => {
  request
    .getCouponsList()
    .then(res => {
      store.commit('setCouponsList', res.data);
    });
}

// 复制到剪切板
export const copyClipboard = (data) => {
  wx.setClipboardData({
    data: data,
    success: (res) => {
    }
  })
}

// 请求所有订单列表
export const getOrderList = () => {
  request
    .getOrderList({
      status: this.active,
      page: 1,
      page_num: 9999
    })
    .then(res => {
      let time = setTimeout(() => {
        clearTimeout(time);
        store.commit('setOrderList', res.data);
      }, 300);
    });
}

// 请求所有订单列表
export const getDate = (date) => {
  if (date) {
    var d = new Date(date * 1000);
    var curr_date = d.getDate();
    var curr_month = d.getMonth() + 1;
    var curr_year = d.getFullYear();
    var curr_hours = d.getHours();
    var curr_minutes = d.getMinutes();
    String(curr_month).length < 2 ? (curr_month = "0" + curr_month) : curr_month;
    String(curr_date).length < 2 ? (curr_date = "0" + curr_date) : curr_date;
    String(curr_hours).length < 2 ? (curr_hours = "0" + curr_hours) : curr_hours;
    String(curr_minutes).length < 2 ? (curr_minutes = "0" + curr_minutes) : curr_minutes;
    // var yyyyMMdd = curr_year + "-" + curr_month + "-" + curr_date + ' ' + curr_hours + ':' + curr_minutes;
    var yyyyMMdd = curr_year + "-" + curr_month + "-" + curr_date;
    return yyyyMMdd;
  }
}

export const getSubsLists = () => {
  // 列表
  request
    .getSubsLists({
      page: 1,
      page_num: 9999
    }).then(res => {
      store.commit('setSubsLists', res.data)
    });
}

export const getSbsOrderLists = () => {
  // 获取订单列表
  request
    .getSbsOrderLists({
      page: 1,
      page_num: 9999
    })
    .then(res => {
      store.commit('setSbsOrderLists', res.data)
    });
}


// 下拉刷新更新的内容
export const onPullDownRefresh = () => {
  getSubsLists()
  getSbsOrderLists()
  getCollects()
  getIndexIndex()
  getMallList()
  getMallCartList()
  getCouponsList()
  getUserInfo()
}



export const handlerCoupons = (option) => {
  request.getCoupons({
    coupon_id: option.id
  }).then(res => {
    $Toast({
      content: res.message,
      type: res.code == 0 ? "success" : "warning"
    });
    onPullDownRefresh()
  }).catch(res => {
    $Toast({
      content: res.message,
      type: res.code == 0 ? "success" : "warning"
    });
  })
}

Vue.prototype.$handlerCoupons = handlerCoupons


// 支付模块
export const wxPay = (option) => {
  return new Promise((resolve, reject) => {
    // 调用支付接口，如果是余额支付，就直接支付了，如果是微信支付，则吊起微信支付
    request.pay(option).then(res => {
      if (res.data.result) {
        const {
          timeStamp,
        } = res.data.result
        // 吊起微信支付
        wx.requestPayment({
          ...res.data.result,
          timeStamp: timeStamp + '',
          success: (success) => {
            $Toast({
              content: "支付成功",
              type: "success"
            });
            resolve(success)
          },
          fail: (error) => {
            $Toast({
              content: "支付失败",
              type: "warning"
            });
            reject(error)
          }
        })
      } else {
        $Toast({
          content: res.message,
          type: res.code == 0 ? "success" : "warning"
        });
        resolve(res.data)
      }
    }).catch(error => {
      reject(error)
    });
  })

}
